package com.liveinstars.activity.business.clockin.service.impl;

import com.baidu.unbiz.easymapper.MapperFactory;
import com.liveinstars.activity.business.clockin.repository.facade.IClockinService;
import com.liveinstars.activity.business.clockin.repository.po.Clockin;
import com.liveinstars.activity.business.clockin.service.ClockInService;
import com.liveinstars.api.dto.ClockinDto;
import com.onepiece.shipelves.common.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("clockinService")
public class ClockInServiceImpl implements ClockInService {

    private final static Logger logger = LoggerFactory.getLogger(ClockInServiceImpl.class);

    @Autowired
    IClockinService clockinService;

    @Autowired
    ClockInFactory clockInFactory;

    @Override
    public ClockinDto getDetailById(Integer id) {
        Clockin clockin = clockinService.getById(id);

        if(clockin != null){
            ClockinDto detailDto = MapperFactory.getCopyByRefMapper().mapClass(Clockin.class, ClockinDto.class).register()
                    .map(clockin, ClockinDto.class);
            return detailDto;
        }
        return null;
    }

    @Override
    public ClockinDto getMyClockIn(Integer userId, Integer activityId) {
        Clockin clockin = clockinService.getMyClockIn(userId, activityId);

        if(clockin != null){
            ClockinDto detailDto = clockInFactory.getClockInDto(clockin);
            return detailDto;
        }
        return null;
    }

    @Override
    public ClockinDto createClockIn(ClockinDto dto) {
        Clockin clockin = new Clockin();
        clockin.setActivityId(dto.getActivityId());
        clockin.setUserId(dto.getUserId());
        clockin.setClockinTime(DateUtil.getGeneralNowTime());
        clockin.setCreateTime(DateUtil.getNowTime());
        clockin.setLocationX(dto.getLocationX());
        clockin.setLocationY(dto.getLocationY());
        boolean save = clockinService.save(clockin);
        if(save){
            clockin = clockinService.getById(dto.getId());
            if(clockin != null){
                ClockinDto detailDto = clockInFactory.getClockInDto(clockin);
                return detailDto;
            }
        }
        return null;
    }


}
